package com.niknik.deberc.core;

import com.niknik.woody.utils.cards.Card;
import com.niknik.woody.utils.cards.CardSuit;
import com.niknik.woody.utils.cards.CardVolume;

import java.util.Comparator;
import java.util.List;

/**
 * User: mllmn
 * Date: 11.08.11
 * Time: 4:08
 */
public class CardComparator
  implements Comparator<Card> {
  private final CardSuit _trump;

  public CardComparator(CardSuit trump) {
    this._trump = trump;
  }

  @Override
  public int compare(Card o1, Card o2) {
    if ((o1.getSuit() == this._trump) && (o2.getSuit() != this._trump)) {
      return 1;
    }
    if ((o2.getSuit() == this._trump) && (o1.getSuit() != this._trump)) {
      return -1;
    }
    int card1Points = getCardPoints(o1);
    int card2Points = getCardPoints(o2);
    if (o1.getSuit() != o2.getSuit()) {
      return 1;
    }
    int result = card1Points - card2Points;
    if (result == 0) {
      if (o1.getSuit() == o2.getSuit()) {
        switch (o1.getVolume()) {
          case _7:
            return -1;
          case _8:
            switch (o2.getVolume()) {
              case _7:
                return 1;
              case _9:
                return -1;
            }
            break;
          case _9:
            return 1;
        }
      }
      else {
        return 1;
      }
    }
    return result > 0
      ? 1
      : -1;
  }

  public boolean validateMove(Card card1, Card card2, List<Card> playerDeck) {
    CardSuit suit1 = card1.getSuit();
    CardSuit suit2 = card2.getSuit();
    int card1Points = this.getCardPoints(card1);
    int card2Points = this.getCardPoints(card2);
    if (suit1 == suit2) {
      if (suit1 == this._trump) {
        if (card1Points > card2Points) {
          int max = card2Points;
          for (Card card : playerDeck) {
            if (card == card2) {
              continue;
            }
            if (card.getSuit() == this._trump) {
              if (this.getCardPoints(card) > max) {
                max = this.getCardPoints(card);
              }
            }
          }
          if (max > this.getCardPoints(card1)) {
            return false;
          }
        }
      }
    }
    else {
      for (Card card : playerDeck) {
        if (card == card2) {
          continue;
        }
        if (card.getSuit() == suit1) {
          return false;
        }
      }
      if (suit2 != this._trump) {
        for (Card card : playerDeck) {
          if (card == card2) {
            continue;
          }
          if (card.getSuit() == this._trump) {
            return false;
          }
        }
      }
    }
    return true;
  }

  public int getCardPoints(Card card) {
    int result = DebercHelper.getCardPoints(card);
    if (card.getSuit() == this._trump) {
      if (card.getVolume() == CardVolume.J) {
        result += 18;
      }
      else if (card.getVolume() == CardVolume._9) {
        result += 14;
      }
    }
    return result;
  }
}

